package com.itheima.quartz;
import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Set;

@Component
public class QiniuyunCleanImage {

    @Component
    public class run {

        //因为要引入 Redis 缓存技术中的JedisPoll  所以要引入@Autowired注解
        @Autowired
        private JedisPool jedisPool;

        public void run() {
            //先获取连接池对象再说
            Jedis resource = jedisPool.getResource();
            Set<String> sdiff = resource.sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
            if (sdiff.size() > 0) {
                List<String> list = QiniuUtils.setToArrayList(sdiff);//  set集合转换 List
                String arr[] = new String[]{};//  定义的泛型
                String[] array = list.toArray(arr);  //  集合转换  数组
                for (String filename : array) {
                    QiniuUtils.deleteFileFromQiniu(filename); //  删除七牛云上的多余图片
                }
                //  resource.srem(RedisConstant.SETMEAL_PIC_RESOURCES,array); //   删除redis多余图片信息
                resource.del(RedisConstant.SETMEAL_PIC_RESOURCES);  //  删除 redis上的两个集合
                resource.del(RedisConstant.SETMEAL_PIC_DB_RESOURCES);
            }
            System.out.println("----running----");
            resource.close();
//        jedisPool.close();    全局变量 不要关闭连接池对象

        }
    }

}
